package tools;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockbasedRequestIdGenerator implements CircularSeqGenerator {

    private static final short SEQ_UPPER_LIMIT = 999;
    private short sequence = -1;
    private final Lock lock = new ReentrantLock();

    private LockbasedRequestIdGenerator() {
    }

    @Override
    public short nextSequence() {
        lock.lock();
        try {
            if (sequence >= SEQ_UPPER_LIMIT) {
                sequence = 0;
            } else {
                sequence++;
            }
            return sequence;
        } finally {
            lock.unlock();
        }
    }

}
